<template>
  <el-dialog :title="info.title" :visible="dialogVisible" width="30%" :before-close="handleClose">
    <strong>请输入类目:</strong>
    <el-input v-model="input" placeholder="请输入内容"></el-input>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submitHandle()">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: ["info", "type"],
  data() {
    return {
      dialogVisible: false,
      input: "",
    };
  },
  methods: {
    handleClose() {
      this.dialogVisible = false;
    },
    submitHandle() {
      if (this.type == 1) {
        this.insertItemCategory({ cid: this.info.cid, name: this.input });
      } else if (this.type == 2) {
        this.updateCategory({ id: this.info.id, name: this.input });
      } else {
        this.insertCategory({ name: this.input });
      }
    },
    async insertCategory(params) {
      let res = await this.$api.insertCategory(params);
      if (res.data.status == 200) {
        this.dialogVisible = false;
        this.$emit("updateViews");
        this.$message({
          message: "添加成功",
          type: "success",
        });
      }
    },
    async updateCategory(params) {
      let res = await this.$api.updateCategory(params);
      if (res.data.status == 200) {
        this.dialogVisible = false;
        this.$emit("updateViews");
        this.$message({
          message: "修改成功",
          type: "success",
        });
      }
    },
    async insertItemCategory(params) {
      let res = await this.$api.insertItemCategory(params);
      if (res.data.status == 200) {
        this.dialogVisible = false;
        this.$emit("updateViews");
        this.$message({
          message: "添加成功",
          type: "success",
        });
      }
    },
  },
};
</script>

<style></style>
